<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class TagsController extends BaseController{

	public function actionIndex(){
		$type = $_GET['type']?1:0;
		$msg['status']=0;
		$model = new UserTages('create');
		$forword_tags = Tags::getForword();
		// print_r($forword_tags);
		$hot_tags = Tags::getHot();
		// print_r($hot_tags);
		$rs = Yii::app()->db->createCommand()
		->select('a.id,a.tag_id,b.tag_name')
		->from('user_tags a')
		->leftJoin('tags b','a.tag_id=b.id')
		->where('a.uid=? and a.type=?',array($this->uid,$type))
		->query();

		if(isset($_POST['UserTages']['taglist'])){
			// print_r($type);exit;
			$model->attributes = $_POST['UserTages'];
			if($model->validate()){
				$tmp = $_POST['UserTages']['taglist'];
				if(strpos($tmp, ',') || strpos($tmp, ',')){
					$tags = str_replace('，', ',', $tmp);
					$tags = explode(',', $tags);
				}else $tags = array($tmp);

				$all_num = count($tags)+$rs->count();
				if($all_num>20){
					$msg['status']=1;
					$msg['desc']="您已经有".$rs->count()."个标签,本次还能添加最多".(20-$rs->count()).'个';
				}else{
					$transcation = Yii::app()->db->beginTransaction();
					try {
						foreach($tags as $value){
							if(!$value)continue;
							//查看此标签是否已经存在标签基表里
							$tag = Tags::model()->findByAttributes(array('tag_name'=>$value));
							if($tag){
								$tag->hot = $tag->hot+1;
							}else{
								$tag = new Tags;
								$value = trim($value);
								$tag->tag_name = $value;
							}
							if($tag->save()){
								$model = new UserTages('create');
								$model->uid = $this->uid;
								$model->tag_id = $tag->id;
								$model->ctime = date('Y-m-d H:i:s');
								$model->type = $type;
								$model->insert();
							}
							
						}
						$transcation->commit();
						$this->refresh();
						
						
					} catch (Exception $e) {
						$transcation->rollback();
						$msg['status']=1;
					}
				}
			}
		}
		$this->render('index',array('rs'=>$rs,'model'=>$model,'msg'=>$msg,'hot_tags'=>$hot_tags,'forword_tags'=>$forword_tags));
	}



	public function actionDel(){
		if(Yii::app()->request->isAjaxRequest){
			if(UserTages::del($_POST['id'])){
				$msg['status']=0;
			}else{
				$msg['status']=1;
				$msg['desc']='标签删除失败！';
			}
			echo json_encode($msg);
		}
	}

	public function actionAdd(){
		if(Yii::app()->request->isAjaxRequest){
			$msg['status']=1;
			$msg['desc']='标签添加失败！';
			$type=0;

			$rs = Yii::app()->db->createCommand()
			->select('a.id,a.tag_id,b.tag_name')
			->from('user_tags a')
			->leftJoin('tags b','a.tag_id=b.id')
			->where('a.uid=? and a.type=?',array($this->uid,$type))
			->query();
			if($rs->count()>19){
				$msg['status']=1;
				$msg['desc']="您已经有20个标签,最多20个";
			}else{
				$model = new UserTages('create');
				$model->taglist='in';
				$model->uid = $this->uid;
				$model->tag_id = $_POST['id'];
				$model->ctime = date('Y-m-d H:i:s');
				$model->type = $type;
				$tag = Tags::model()->findByAttributes(array('id'=>$_POST['id']));
				$tag->hot = $tag->hot+1;
				$tag->save();

				if($model->save()){
					$msg['status']=0;
					$msg['desc']='标签添加成功！';
				}
			}
			
			echo json_encode($msg);
		}
	}

	
}